package com.ltxtiyu.video.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ltxtiyu.video.entity.HotWord;
import com.ltxtiyu.video.service.ms.SearchService;

import com.ltxtiyu.video.tool.StringUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("searchController")
@Controller
public class SearchController {




    @Reference
    private SearchService ss;

    /**
     * 搜索条件查询列表
     * @param page
     * @param limit
     * @param word
     * @param name
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping(value = "searchList",method = RequestMethod.GET)
    public ResponseEntity searchList(int page, int limit ,Integer userId,String word,String name,String startTime,String endTime){
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date startData = null;
            if (StringUtil.isNotBlank(startTime)){
                startData = format.parse(startTime);
            }

            Date endData = null;
            if (StringUtil.isNotBlank(endTime)){
                endData = format.parse(endTime);
            }
            Map<String,Object> result = ss.searchList(page,limit,word,name,startData,endData,userId);

            return new ResponseEntity(result,HttpStatus.OK);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

    /**
     * 热搜条件查询列表
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping(value = "paramlist",method = RequestMethod.GET)
    public ResponseEntity<Map<String ,Object>> queryById(int page, int limit){
        try {
            Map<String,Object> map = new HashMap<>();
            map.put("page",(page-1)*limit);
            map.put("row",limit);

            Map<String,Object> result = ss.selectParamList(map);

            return ResponseEntity.ok(result);
        }catch (Exception e){
            e.printStackTrace();
            return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }

    }

    /**
     * 热搜榜
     * @return
     */
    @RequestMapping(value = "todayHotSearch",method = RequestMethod.GET)
    public ResponseEntity todayHotSearch(){
        try {
            Map<String,Object> result = ss.todayHotSearch();

            return new ResponseEntity(result,HttpStatus.OK);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 新增热搜词
     * @param name
     * @param searchIndex
     * @return
     */
    @RequestMapping(value = "insert")
    public ResponseEntity insert(String name,Long searchIndex){
        try {
            if (StringUtil.isNullOrEmpty(name)){
                return new ResponseEntity(HttpStatus.BAD_REQUEST);
            }
            HotWord hotWord = new HotWord();
            hotWord.setName(name);
            hotWord.setSearchIndex(searchIndex);
            ss.insert(hotWord);

            return ResponseEntity.status(HttpStatus.CREATED).build();
        }catch(Exception e){
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }

    }


    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "delById")
    public ResponseEntity<Void> delById(Integer id){
        try {
            if (id.longValue() <1){
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }
            ss.delByID(id);
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }

    }





}
